#!/usr/bin/env python
# -*- coding: utf-8 -*-
#sys.exit(0)

import os
import re
import time
import sys
from threading import Thread
from pygnet import server
import socket
from random import choice
from pickle import dumps, loads
from pprint import pprint

BUFFSIZE = 1024                 # for socket.send & recv commands
BACKLOG = 2                     # number of clients supported by server

class ChatServer(Thread):
    def __init__ (self,ip,port):
        Thread.__init__(self)
        self.ip = ip
        self.port = port
        self.sender = ChatServer_sender(ip,port)
        self.receiver = ChatServer_receiver(ip,port)
    def run(self):
        print "Hello, I am the ChatServer on ip:"+str(self.ip)+" port:" +str(self.port)
        self.sender.start()
        self.receiver.start()

class GameServer(Thread):
    def __init__ (self,ip,port):
        Thread.__init__(self)
        self.ip = ip
        self.port = port
        self.sender = GameServer_sender(ip,port)
        self.receiver = GameServer_receiver(ip,port)
    def run(self):
        print "Hello, I am the GameServer on ip:"+str(self.ip)+" port:" +str(self.port)
        self.sender.start()
        self.receiver.start()

class ChatClient(Thread):
    def __init__ (self,ip,port):
        Thread.__init__(self)
        self.ip = ip
        self.port = port
        self.sender = ChatClient_sender(ip,port)
        self.receiver = ChatClient_receiver(ip,port)
    def run(self):
        print "Hello, I am the ChatClient on ip:"+str(self.ip)+" port:" +str(self.port)
        self.sender.start()
        self.receiver.start()

class GameClient(Thread):
    def __init__ (self,ip,port):
        Thread.__init__(self)
        self.ip = ip
        self.port = port
        self.sender = GameClient_sender(ip,port)
        self.receiver = GameClient_receiver(ip,port)
    def run(self):
        print "Hello, I am the GameClient on ip:"+str(self.ip)+" port:" +str(self.port)
        self.sender.start()
        self.receiver.start()

class ChatServer_receiver(Thread):
    def __init__ (self,ip,port):
        Thread.__init__(self)
        self.ip = ip
        self.port = port
    def run(self):
        print "Hello, I am the ChatServer_receiver on ip:"+str(self.ip)+" port:" +str(self.port)

class GameServer_receiver(Thread):
    def __init__ (self,ip,port):
        Thread.__init__(self)
        self.ip = ip
        self.port = port
    def run(self):
        print "Hello, I am the GameServer_receiver on ip:"+str(self.ip)+" port:" +str(self.port)

class ChatClient_receiver(Thread):
    def __init__ (self,ip,port):
        Thread.__init__(self)
        self.ip = ip
        self.port = port
    def run(self):
        print "Hello, I am the ChatClient_receiver on ip:"+str(self.ip)+" port:" +str(self.port)
            # Create a socket (SOCK_STREAM means a TCP socket)
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.bind((self.ip,self.port))
        sock.listen(BACKLOG)
        serverip, serverport = sock.getsockname()
        print("Running at %s, %s" % (serverip, serverport))
        print("Waiting for player...")
        client, address = sock.accept()
        clientip, clientport = address

class GameClient_receiver(Thread):
    def __init__ (self,ip,port):
        Thread.__init__(self)
        self.ip = ip
        self.port = port
    def run(self):
        print "Hello, I am the GameClient_receiver on ip:"+str(self.ip)+" port:" +str(self.port)

class ChatServer_sender(Thread):
    def __init__ (self,ip,port):
        Thread.__init__(self)
        self.ip = ip
        self.port = port
    def run(self):
        print "Hello, I am the ChatServer_sender on ip:"+str(self.ip)+" port:" +str(self.port)

class GameServer_sender(Thread):
    def __init__ (self,ip,port):
        Thread.__init__(self)
        self.ip = ip
        self.port = port
    def run(self):
        print "Hello, I am the GameServer_sender on ip:"+str(self.ip)+" port:" +str(self.port)

class ChatClient_sender(Thread):
    def __init__ (self,ip,port):
        Thread.__init__(self)
        self.ip = ip
        self.port = port
    def run(self):
        print "Hello, I am the ChatClient_sender on ip:"+str(self.ip)+" port:" +str(self.port)

class GameClient_sender(Thread):
    def __init__ (self,ip,port):
        Thread.__init__(self)
        self.ip = ip
        self.port = port
    def run(self):
        print "Hello, I am the GameClient_sender on ip:"+str(self.ip)+" port:" +str(self.port)

#clients=["alpha","beta","gamma"]
#servers=[0]

#ChatServerList=[]
#GameServerList=[]

#for i in servers:
    #current = ChatServer(i)
    #ChatServerList.append(current)
    #current.start()
    #current = GameServer(i)
    #GameServerList.append(current)
    #current.start()

#ChatClientList=[]
#GameClientList=[]

#for i in clients:
    #current = ChatClient(i)
    #ChatClientList.append(current)
    #current.start()
    #current = GameClient(i)
    #GameClientList.append(current)
    #current.start()

#ThreadList=ChatServerList+GameServerList+ChatClientList+GameClientList

#for T in ThreadList:
    #T.join()
